package com.hotel.controller;

import com.hotel.pojo.Order;
import com.hotel.vo.OrderVo;
import com.hotel.vo.UserPassVo;
import com.hotel.pojo.User;
import com.hotel.service.UserService;
import com.hotel.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 注册
    @PostMapping("register/{name}")
    public String userRegister(@PathVariable("name") String name,
                               @RequestBody UserPassVo passVo) {
        User user = new User();
        user.setName(MD5Util.encrypt(name));
        user.setPhone(MD5Util.encrypt(passVo.getPhone()));
        user.setUsername(MD5Util.encrypt(passVo.getUsername()));
        user.setPassword(MD5Util.encrypt(passVo.getPassword()));
        user.setStatus(0);
        if (userService.registerUser(user)) {
            return "用户注册成功！";
        } else {
            return "用户名或姓名存在，注册失败！";
        }
    }

    // 登录
    @PostMapping("login/{name}")
    public String userLogin(@PathVariable("name") String name,
                            @RequestBody UserPassVo userPassVo) {
        User user = new User();
        user.setName(MD5Util.encrypt(name));
        user.setUsername(MD5Util.encrypt(userPassVo.getUsername()));
        user.setPassword(MD5Util.encrypt(userPassVo.getPassword()));
        // 先检查账号密码是否正确
        if (userService.checkUser(user)) {
            // 再看是否已经登录
            if (userService.isLogin(MD5Util.encrypt(name))) {
                return "请勿重复登录！";
            } else {
                userService.loginUser(MD5Util.encrypt(name));
                return "登陆成功！";
            }
        } else {
            return "用户不存在，登陆失败！";
        }
    }

    // 注销
    @PostMapping("exit/{name}")
    public String userExit(@PathVariable("name") String name) {
        if (userService.findUserByName(MD5Util.encrypt(name))) {
            userService.exitUser(MD5Util.encrypt(name));
            return "注销成功！";
        } else {
            return "用户不存在！";
        }
    }

    // 维护基本信息
    @PostMapping("{name}/update")
    public String updateUser(@PathVariable("name") String name,
                             @RequestBody UserPassVo userPassVo) {
        // 先检查用户是否登录
        if (!userService.isLogin(MD5Util.encrypt(name))) {
            return "请先登录！";
        }
        if (userService.findUserByName(MD5Util.encrypt(name))) {
            User user = new User();
            user.setName(MD5Util.encrypt(name));
            user.setPhone(MD5Util.encrypt(userPassVo.getPhone()));
            user.setUsername(MD5Util.encrypt(userPassVo.getUsername()));
            user.setPassword(MD5Util.encrypt(userPassVo.getPassword()));
            userService.updateUser(user);
            return "修改成功！";
        } else {
            return "用户不存在，修改失败！";
        }
    }

    // 查看酒店信息
    @GetMapping("{name}/findallhotel")
    public String findAllHotel(@PathVariable("name") String name) {
        // 先检查用户是否登录
        if (!userService.isLogin(MD5Util.encrypt(name))) {
            return "请先登录！";
        }
        if (userService.findUserByName(MD5Util.encrypt(name))) {
            return userService.findAllHotel().toString();
        } else {
            return "用户不存在，查看失败！";
        }
    }

    // 预订酒店
    @PostMapping("{name}/orderhotel")
    public String orderHotel(@PathVariable("name") String name,
                             @RequestBody OrderVo orderVo) {
        // 先检查用户是否登录
        if (!userService.isLogin(MD5Util.encrypt(name))) {
            return "请先登录！";
        }
        if (userService.findUserByName(MD5Util.encrypt(name))) {
            Order order = new Order();
            order.setUserName(MD5Util.encrypt(name));
            order.setHotelName(orderVo.getHotelName());
            order.setType(orderVo.getType());
            order.setStartTime(orderVo.getStartTime());
            order.setEndTime(orderVo.getEndTime());
            order.setGuestNumber(orderVo.getGuestNumber());
            order.setHasChild(orderVo.getHasChild());
            order.setRoomCount(orderVo.getRoomCount());
            boolean b = userService.orderHotel(order);
            if (b) {
                return "预定成功！";
            } else {
                return "无房间，预定失败！";
            }
        } else {
            return "用户不存在，预定失败！";
        }
    }

    // 浏览订单
    @GetMapping("{name}/findorder")
    public String findOrderByName(@PathVariable("name") String name) {
        // 先检查用户是否登录
        if (!userService.isLogin(MD5Util.encrypt(name))) {
            return "请先登录！";
        }
        if (userService.findUserByName(MD5Util.encrypt(name))) {
            return userService.findOrderByName(MD5Util.encrypt(name)).toString();
        } else {
            return "用户不存在！";
        }
    }
}
